package com.suncp.scheduler;

import com.suncp.job.PrintWordsJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * Notice:
 *
 * @author scp
 * @version 1.0
 * @date 2021/3/10
 * @since 1.0
 */
public class MyScheduler2 {
    public static void main(String[] args) throws SchedulerException, InterruptedException {

        // 1.创建调度器
        StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        // 2.创建JobDetail实例，并与PrintWordsJob类绑定(job执行内容)
        JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class).withIdentity("job1", "group1")
                .usingJobData("jobDetail1", "这个Job用来测试的.") // 测试jobDataMap存值，job中JobExecutionContext可以获取存储的信息 【可有可无】
                .build();

        // 3.构建Trigger实例，设置cron表达式
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger", "triggerGroup1")
                .usingJobData("trigger1", "这是jobDetail的trigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ? ")) // cron表达式 每两秒执行一次
                .build();

        // 4.执行
        scheduler.scheduleJob(jobDetail, cronTrigger);
        System.out.println("==========scheduler start !==============");
        scheduler.start();
    }
}
